package com.digicom.service.topic.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digicom.common.constant.Constants;
import com.digicom.common.entity.Topic;
import com.digicom.common.exception.ServiceException;
import com.digicom.common.mapper.TopicMapper;
import com.digicom.service.topic.TopicService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 专题服务实现类
 */
@Slf4j
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private KafkaTemplate<String, Object> kafkaTemplate;

    @Override
    public Page<Topic> listTopicPage(Topic topic, Page<Topic> page) {
        return topicMapper.selectTopicPage(page, topic);
    }

    @Override
    public List<Topic> listPublishedTopics() {
        return topicMapper.selectPublishedTopics();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveTopic(Topic topic) {
        if (topic == null || StringUtils.isBlank(topic.getTitle())) {
            throw new ServiceException("专题信息不完整");
        }

        // 设置状态为草稿，未同步到APP
        if (StringUtils.isBlank(topic.getStatus())) {
            topic.setStatus(Constants.TOPIC_STATUS_DRAFT);
        }
        topic.setSyncApp("0");

        boolean result;
        if (topic.getTopicId() != null) {
            // 更新专题
            result = updateById(topic);
        } else {
            // 新增专题
            result = save(topic);
        }

        if (!result) {
            throw new ServiceException("保存专题失败");
        }
        
        return topic.getTopicId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean publishTopic(Long topicId) {
        if (topicId == null) {
            throw new ServiceException("参数不能为空");
        }

        Topic topic = getById(topicId);
        if (topic == null) {
            throw new ServiceException("专题不存在");
        }

        // 设置为已发布状态
        topic.setStatus(Constants.TOPIC_STATUS_PUBLISHED);
        // 设置发布时间
        topic.setPublishTime(new Date());
        
        boolean result = updateById(topic);
        if (result) {
            // 发布成功后，同步到APP
            syncTopicToApp(topicId);
        }
        
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean syncTopicToApp(Long topicId) {
        if (topicId == null) {
            throw new ServiceException("参数不能为空");
        }

        Topic topic = getById(topicId);
        if (topic == null) {
            throw new ServiceException("专题不存在");
        }

        try {
            // 发送消息到Kafka，通知APP同步专题数据
            kafkaTemplate.send(Constants.TOPIC_TOPIC_SYNC, topic);
            
            // 更新同步状态
            topic.setSyncApp("1");
            return updateById(topic);
        } catch (Exception e) {
            log.error("同步专题到APP失败", e);
            throw new ServiceException("同步专题到APP失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteTopics(Long[] topicIds) {
        if (topicIds == null || topicIds.length == 0) {
            throw new ServiceException("参数不能为空");
        }

        // 逻辑删除专题
        LambdaQueryWrapper<Topic> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Topic::getTopicId, Arrays.asList(topicIds));
        return remove(wrapper);
    }

    @Override
    public Topic getTopicDetail(Long topicId) {
        if (topicId == null) {
            throw new ServiceException("参数不能为空");
        }

        return topicMapper.selectTopicDetail(topicId);
    }
} 